package vn.vfriends.id.bean;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.id.jpa.entity.User;
import vn.vfriends.id.service.AccountService;
import vn.vfriends.id.service.UserService;

/**
 *
 * @author tuan@vfriends.vn
 */
@Named
@ConversationScoped
public class ProfileBean implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(ProfileBean.class);
    
    private User user;
    
    @Inject private Conversation conversation;
    
    @Inject private UserService userService;
    
    @Inject private AccountService accountService;
    
    @PostConstruct
    public void init(){
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
            HttpSession session = request.getSession();
            user = (User) session.getAttribute("LOGGED_USER");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    public long getAmount() {
        try {
            if(this.user == null || this.user.getCif() == null || this.user.getCif().length() == 0)
                return 0;
            return accountService.getBalance(this.user.getCif());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
            return 0;
        }
    }

    public Conversation getConversation() {
        return conversation;
    }
    
    public String preEditAction() {
        String outcome = null;
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            
            if(this.user != null){
                outcome = "changeprofile?faces-redirect=true";
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Profile is not exist", ""));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));

        }
        return outcome;
    }
    
    public String cancelAction() {
        conversation.end();
        return "profile?faces-redirect=true";
    }
    
    public String updateAction() {
        try {
            this.userService.edit(user);
            conversation.end();
            
            // Update session
            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
            HttpSession session = request.getSession();
            session.setAttribute("LOGGED_USER", user);
            
            return "profile?faces-redirect=true";
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            return null;
        }
    }
}
